package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserPassRoad;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserRoadDaoImpl extends TempDataObjectDao<UserPassRoad> {

	public UserPassRoad getUserRoad(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_pass_road where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserPassRoad data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserPassRoad data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_pass_road(userId,passId,passProgress,sessionId,modifyTime,buyBuffTimes,itemBuyBuffTimes,eventData,transportId,isEventUpdate,maxRequestTimesOneSecond) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getPassId());
			call.setInt(i++, data.getPassProgress());
			call.setInt(i++, data.getSessionId());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getBuyBuffTimes());
			call.setInt(i++, data.getItemBuyBuffTimes());
			call.setString(i++, data.getEventData());
			call.setInt(i++, data.getTransportId());
			call.setBoolean(i++, data.isEventUpdate());
			call.setLong(i++, data.getMaxRequestTimesOneSecond());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserPassRoad data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_pass_road SET passId=?,passProgress=?,sessionId=?,modifyTime=?,buyBuffTimes=?,itemBuyBuffTimes=?,eventData=?,isEventUpdate=?,transportId=?,maxRequestTimesOneSecond=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getPassId());
			call.setInt(i++, data.getPassProgress());
			call.setInt(i++, data.getSessionId());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getBuyBuffTimes());
			call.setInt(i++, data.getItemBuyBuffTimes());
			call.setString(i++, data.getEventData());
			call.setBoolean(i++, data.isEventUpdate());
			call.setInt(i++, data.getTransportId());
			call.setLong(i++, data.getMaxRequestTimesOneSecond());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserPassRoad parseData(ResultSet rs) throws SQLException {
		UserPassRoad data = new UserPassRoad();
		data.setUserId(rs.getLong("userId"));
		data.setPassId(rs.getInt("passId"));
		data.setPassProgress(rs.getInt("passProgress"));
		data.setSessionId(rs.getInt("sessionId"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setBuyBuffTimes(rs.getInt("buyBuffTimes"));
		data.setItemBuyBuffTimes(rs.getInt("itemBuyBuffTimes"));
		String eventData = rs.getString("eventData");
		if("".equals(eventData)){
			eventData = "{}";
		}
		data.setEventData(eventData);
		data.setTransportId(rs.getInt("transportId"));
		data.setEventUpdate(rs.getBoolean("isEventUpdate"));
		data.setMaxRequestTimesOneSecond(rs.getLong("maxRequestTimesOneSecond"));
		return data;
	}
}
